package com.fy.util;

import java.io.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 音乐文件解析工具类
 * @author 小叶
 * @version 1.0
 */
public class MusicUtil {
	public static void main(String[] args) {
		String lrcPath = "E:/Projects/Java/MyEclipse/Music/WebRoot/music1/1.lrc";
		String lrc = getLrc(lrcPath);
		System.out.println(lrc);
	}
	/**
	 * 获取音乐歌词
	 * @param lrcPath 歌词路径
	 * @return String 返回歌词
	 */
	public static String getLrc(String lrcPath) {
		// 存储歌词的容器
		StringBuffer sb = new StringBuffer();
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			// 开始读取歌词文件
			fis = new FileInputStream(lrcPath);
			// 获取歌词
			isr = new InputStreamReader(fis, "utf-8");
			// 获取歌词缓冲流，进行读取
			br = new BufferedReader(isr);
			// 建立临时变量
			String temp = null;
			while ((temp = br.readLine()) != null)
				sb.append(temp + "\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fis != null)
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (br != null)
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return sb.toString();
	}
	/**
	 * 读取音乐歌词
	 * @param request 前台请求
	 * @return 返回歌词
	 */
	public static String readLrc(HttpServletRequest request) {
		// 获取音乐名称
		String name = request.getParameter("name");
		// 获取音乐对应的歌词的路径
		String filePath = request.getSession().getServletContext().getRealPath("") + "/" + name + ".lrc";
		return getLrc(filePath);
	}
}